# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import fields, models, api

class ConsulteeType(models.Model):
    # 咨询者类型
    _name = "consult.consultee.type"
    _description = 'ConsulteeType'
    _order = "code"

    code = fields.Char(index=True)
    name = fields.Char(index=True)
    survey_id = fields.Many2one(
        'survey.survey',  string="Default Survey" ,
        domain=[('category', '=', 'consult_consultation')],
        help="该类咨询者的通用 调研表" )

class Channel(models.Model):
    _inherit = 'mail.channel'

    opp_partner_id = fields.Many2one( 'res.partner',  compute='_compute_opp_partner_id' )
    opp_partner_name = fields.Char(related='opp_partner_id.name', string="Opp Name", )

    @api.depends('channel_partner_ids')
    def _compute_opp_partner_id(self):
        for rec in self:
            if self.env.user.partner_id in rec.channel_partner_ids:
                ptn = rec.channel_partner_ids - self.env.user.partner_id
                rec.opp_partner_id = ptn and ptn[0]
            else:
                rec.opp_partner_id = self.env['res.partner']


class Partner(models.Model):
    _inherit = "res.partner"

    private_channel_ids = fields.Many2many('mail.channel', string='Private Channels',
        compute='_compute_private_channel_ids')

    opp_partner_ids = fields.Many2many('res.partner', compute='_compute_private_channel_ids' )

    @api.depends('channel_ids')
    def _compute_private_channel_ids(self):
        domain = [
            ('channel_type', '=', 'chat'),
            ('public', '=', 'private'),
            ('name', '!=', 'OdooBot'),
            ('channel_partner_ids', 'in', self.env.user.partner_id.ids)
        ]

        for rec in self:
            cnls = self.env['mail.channel'].search(domain)
            rec.private_channel_ids = cnls.filtered(lambda item: len(item.channel_partner_ids)==2 )
            rec.opp_partner_ids = rec.private_channel_ids.mapped('opp_partner_id')

    private_channel_id = fields.Many2one('mail.channel', string='Private Channel',
        compute='_compute_private_channel_id')

    @api.depends('channel_ids')
    def _compute_private_channel_id(self):
        domain = [
            ('channel_type', '=', 'chat'),
            ('public', '=', 'private'),
            ('name', '!=', 'OdooBot'),
            ('channel_partner_ids', 'in', self.env.user.partner_id.ids),
        ]

        for rec in self:
            domain = domain + [('channel_partner_ids', 'in', rec.ids)]

            cnls = self.env['mail.channel'].search(domain)
            cnls = cnls.filtered(lambda item: len(item.channel_partner_ids)==2 )
            rec.private_channel_id = cnls and cnls[0]

    # -------------------------------------------------------------------------
    # for Consultee
    # -------------------------------------------------------------------------

    def _default_get_consultee_type_id(self):
        ref_id = 'consult.consult_consultee_type_adult_male'
        return self.env['ir.model.data'].xmlid_to_res_id(ref_id )

    consultee_type_id = fields.Many2one( 'consult.consultee.type', default=_default_get_consultee_type_id  )
    consultee_type_code = fields.Char(related='consultee_type_id.code', store=True)
    survey_id = fields.Many2one('survey.survey', related='consultee_type_id.survey_id', string="Default Survey" )
    consultant_ids = fields.Many2many('res.partner', string="Consultants", related='opp_partner_ids' )

    consultee_consultation_ids = fields.One2many('consult.consultation', 'partner_id', string="Consultee Consultations" )


    # -------------------------------------------------------------------------
    # for Consultant
    # -------------------------------------------------------------------------

    consultee_ids = fields.Many2many('res.partner', string="Consultees", related='opp_partner_ids' )
    consult_survey_ids = fields.One2many(
        'consult.survey', 'consultant_id', string=" Consultant Survey" )
    consultant_consultation_ids = fields.One2many('consult.consultation', 'consultant_id', string="Consultant Consultations" )

    # -------------------------------------------------------------------------
    # for both Consultant and Consultee
    # -------------------------------------------------------------------------

    consultation_ids = fields.Many2many('consult.consultation',  string='Consultations', compute='_compute_consultation_ids', )
    
    @api.depends('consultant_consultation_ids', 'consultee_consultation_ids')
    def _compute_consultation_ids(self):
        for rec in self:
            if rec.employee:
                rec.consultation_ids = rec.consultant_consultation_ids
            else:
                rec.consultation_ids = rec.consultee_consultation_ids


    consultation_id = fields.Many2one(
        'consult.consultation', 
        string='Consultation',
        compute='_compute_consultation_id',
        help="the consultation with this partner and current user")

    @api.depends('consultation_ids')
    def _compute_consultation_id(self):
        ptn = self.env.user.partner_id

        for rec in self:
            if rec.employee and not ptn.employee:
                consultation = self.env['consult.consultation']._consultation_get(rec, ptn)
            elif not rec.employee and ptn.employee:
                consultation = self.env['consult.consultation']._consultation_get(ptn, rec)
            else:
                consultation = self.env['consult.consultation']

            rec.consultation_id = consultation
